<?php 
namespace app\api\service\v1;
/*
 * content : 注册服务类 
 * Author  : Songyicong 
 * time    : 2018-04-30 15:44:26 
 * email   : m13726262373@163.com 
 */

 use app\api\service\BaseService;
 use app\api\extra\Email;
 use think\Db;
 use app\api\model\v1\User as UserModel;
use think\Cache;
 class RegistService extends BaseService{

    /** 验证码判断 */
    public function isVerifyEmailCode($params = []){
        $emails = UserModel::isRegistEmail($params['email']);
        if(empty($params['email']) || !isEmail($params['email']) || !$emails)
            return self::sendError(100101, 'email is error',200);
        if(empty($params['code'])){
            return self::sendError(100103,'code is empty',200);
        }
        if(Email::verifyCode('regist_'.$params['email'],$params['code'])){
            // 开始保存用户验证信息
            $emails['state'] = my_str_replace($emails['state'],1,1);
            Db::name('user')->where("id",$emails['id'])->update(['state'=>$emails['state'].""]);
            return self::sendError(100100,'success',200);
        }
        return self::sendError(100105,'code is error',200);
    }

    /** 更新邮件验证码 */
    public function updateEmailCode($params = []){
        if(empty($params['email']) || !isEmail($params['email']))
            return self::sendError(100101, 'email is error',200);
        // 用 email 是否存在
        if(!UserModel::isRegistEmail($params['email'])){
            return self::sendError(100103,'email error',200);
        }
        if(Email::sendRegist($params['email'])){
            return self::sendError(100100,'success',200);
        }
        
        return self::sendError(100000,'unknown error',500);
    }

     /** 邮箱注册 */
     public function setRegistEmail($params = []){
        if(empty($params['email']) || !isEmail($params['email']))
            return self::sendError(100101, 'email is error',200);
        if(empty($params['password'])||empty($params['auth'])||empty($params['login_name']))
            return self::sendError(100102,'the default parameter cannot be empty',200);
        // 用 email 是否存在
        if(UserModel::isRegistEmail($params['email'])){
            return self::sendError(100103,'email expired',200);
        }
        if(UserModel::isRegistLoginName($params['login_name']))
            return self::sendError(100104,'login_name expired',200);

        $data['password'] = md5($params['password']);    
        $data['login_name'] = $params['login_name'];    
        $data['auth_num'] = $params['auth'];    
        $data['email'] = $params['email'];    
        $data['create_time'] = time();    
        $data['ip'] = get_client_ip();    
        $data['last_time'] = time();    
        $data['title'] = empty($params['title'])?'':$params['title'];
        $data['name'] = empty($params['name'])?'':$params['name'];   
        $data['sex'] = empty($params['sex'])?'1':$params['sex'];   
        $data['address'] = empty($params['address'])?'':$params['address'];   
        $data['interge'] = 0;    
        $data['balance'] = 0;    
        $data['vip'] = 0;    
        $data['vip_score'] = 0;    
        $data['state'] = 0;    
        if($uid = Db::name('user')->insertGetId($data) && Email::sendRegist($params['email'])){
            return self::sendError(100100,'success',200);
        }
        return self::sendError(100000,'unknown error',500);
    }
    

 }
